package palmtop.com.palmtop.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import net.grandcentrix.tray.AppPreferences;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.EditionActivity;
import palmtop.com.palmtop.activity.LoginActivity;
import palmtop.com.palmtop.activity.Main2Activity;
import palmtop.com.palmtop.activity.ModPsswordActivity;
import palmtop.com.palmtop.activity.MyApplication;
import palmtop.com.palmtop.activity.MyMessageActivity;
import palmtop.com.palmtop.service.NotificationService;
import palmtop.com.palmtop.service.NotifyService;
import palmtop.com.palmtop.thread.PersonposThread;


public class MyFragment extends Fragment {


    @BindView(R.id.myinfo)
    RelativeLayout myinfo;
    @BindView(R.id.action_modpassword)
    RelativeLayout actionModpassword;
    @BindView(R.id.action_logout)
    RelativeLayout actionLogout;
    @BindView(R.id.action_edition)
    RelativeLayout actionEdition;
    //    private OnFragmentInteractionListener mListener;
    private Unbinder unbinder;
    MyApplication app;

    public MyFragment() {

    }


    public static MyFragment newInstance(String param1, String param2) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        app = (MyApplication) getActivity().getApplication();
//        return inflater.inflate(R.layout.fragment_my, container, false);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_my, null);
        unbinder = ButterKnife.bind(this, view);
        init(view);
        return view;
    }

    private void init(View view) {
    }


    /*public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }*/

    /*@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }*/

   /* @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }*/

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.myinfo)
    public void onMyinfoClicked() {
        Intent a = new Intent(getActivity(), MyMessageActivity.class);
        getActivity().startActivity(a);
    }

    @OnClick(R.id.action_modpassword)
    public void onActionModpasswordClicked() {
        Intent b = new Intent(getActivity(), ModPsswordActivity.class);
        getActivity().startActivity(b);
    }

    @OnClick(R.id.action_logout)
    public void onActionLogoutClicked() {

        //清空permission
        AppPreferences appPreferences = new AppPreferences(getActivity());
        appPreferences.remove("permission");
        appPreferences.clear();

        //=================================
        Intent intent = new Intent();
        intent.setClass(getActivity(), NotifyService.class);
        getActivity().stopService(intent);

        //NotificationService==========================
        Intent intent1 = new Intent();
        intent1.setClass(getActivity(), NotificationService.class);
        getActivity().stopService(intent1);

        //清空所有打开的activity
        Intent i = new Intent(getActivity(), LoginActivity.class);
        startActivity(i);
        app.exitlist();
    }

    @OnClick(R.id.action_edition)
    public void onViewClicked() {
        Intent c =new Intent(getActivity(),EditionActivity.class);
        getActivity().startActivity(c);

    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
